#include "paginator.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QSpacerItem>

#include "util.h"
Paginator::Paginator(int count, QWidget *parent)
    : QWidget{parent}
{
    this->pageCount = count;
    this->setMinimumSize(1270, 32);
    QHBoxLayout *layout = new QHBoxLayout();
    this->setLayout(layout);
    layout->setContentsMargins(0, 0, 10, 0);
    layout->setSpacing(8);

    // 创建分⻚器内部按钮
    // 创建上⼀⻚按钮
    prevPageBtn = new QPushButton();
    prevPageBtn->setIcon(QIcon(":/images/admin/arrow-left.png"));
    setBtnStyle(prevPageBtn);
    // 创建下⼀⻚按钮
    nextPageBtn = new QPushButton();
    nextPageBtn->setIcon(QIcon(":/images/admin/arrow-right.png"));
    setBtnStyle(nextPageBtn);

    // 创建中间⻚⾯按钮
    // 如果⻚⾯数⼩于等于7，不需要折叠按钮，默认显⽰第1⻚
    if (pageCount <= 7) {
        for (int i = 1; i <= pageCount; i++) {
            PageButton* pageBtn = new PageButton(i);
            if(i == 1) {
                pageBtn->setActive(true);
            }
            pages.push_back(pageBtn);
        }
    } else {
        // ⻚⾯超过7⻚，前5个⻚⾯按钮上显⽰1~5数字
        // 第6个按钮为折叠按钮，第7个按钮上显⽰最后⼀⻚
        // 默认显⽰第1⻚
        for(int i = 1 ; i <= 5 ; ++i) {
            PageButton* pageBtn = new PageButton(i);
            if(i == 1) {
                pageBtn->setActive(true);
            }
            pages.push_back(pageBtn);
        }
        // 添加 ... 按钮
        PageButton* foldedBtn = new PageButton(0);  // TODO
        foldedBtn->setFolded(true);
        pages.push_back(foldedBtn);
        // 添加最后⼀⻚按钮
        PageButton* lastBtn = new PageButton(pageCount);
        pages.push_back(lastBtn);
    }

    // 创建两个 QLabel 显⽰⽂字
    QLabel* jumpToLabel = new QLabel("跳至");
    QLabel* pageLabel = new QLabel("页");

    // 创建输⼊框, 输⼊⻚码
    pageEdit = new QLineEdit();
    pageEdit->setFixedSize(QSize(48, 32));
    pageEdit->setAlignment(Qt::AlignLeft);
    pageEdit->setStyleSheet("QLineEdit{"
                            "background-color: #FFFFFF; "
                            "border: 1px solid #D9D9D9; "
                            "border-radius: 2px; "
                            "padding-left: 12px; }");

    // 布局设置
    layout->addStretch(); // 增加⼀个空⽩区域
    layout->addWidget(prevPageBtn);
    for (int i = 0; i < pages.size(); i++) {
        layout->addWidget(pages[i]);
    }
    layout->addWidget(nextPageBtn);
    layout->addWidget(jumpToLabel);
    layout->addWidget(pageEdit);
    layout->addWidget(pageLabel);

    initSignalsAndSlots();
}

void Paginator::setBtnStyle(QPushButton *btn)
{
    btn->setIconSize(QSize(12, 12));
    btn->setFixedSize(QSize(32, 32));
    btn->setStyleSheet("QPushButton {"
                       "color: #000000; "
                       "background-color: #FFFFFF; "
                       "border: 1px solid #D9D9D9; "
                       "border-radius: 2px;}");
}

void Paginator::initSignalsAndSlots()
{
    // 处理上⼀⻚, 下⼀⻚.
    connect(prevPageBtn, &QPushButton::clicked, this, &Paginator::prevPage);
    connect(nextPageBtn, &QPushButton::clicked, this, &Paginator::nextPage);
    // 处理每一个按钮点击效果
    for(auto& pageBtn : pages) {
        connect(pageBtn, &QPushButton::clicked, this, &Paginator::clickPageBtn);
    }
    // 处理输入框
    connect(pageEdit, &QLineEdit::returnPressed, this, [=](){
        int page = pageEdit->text().toInt();
        if(page < 1) {
            page = 1;
        }
        if(page > pageCount) {
            page = pageCount;
        }
        this->jumpToPage(page);
    });
}

void Paginator::jumpToPage(int page)
{
    if(page < 1 || page > pageCount) {
        return ;
    }
    currentPage = page;
    if(pageCount <= 7) {
        for(auto pageBtn : pages) {
            if(pageBtn->getPage() == currentPage) {
                pageBtn->setActive(true);
            } else {
                pageBtn->setActive(false);
            }
        }
    } else {
        // 页数超过7个
        if(currentPage <= 5) {
            // case1: currentPage in [1,5]
            jumpToPageCase1();
        } else if (currentPage >= pageCount - 4){
            // case2: currentPage in [pageCount-4, pageCount]
            jumpToPageCase2();
        } else {
            //case3:
            jumpToPageCase3();
        }
    }
    emit pageChanged(page);
}

void Paginator::jumpToPageCase1()
{
    pages[0]->setPage(1);
    pages[1]->setPage(2);
    pages[2]->setPage(3);
    pages[3]->setPage(4);
    pages[4]->setPage(5);
    pages[5]->setPage(6);
    pages[6]->setPage(pageCount);

    for(auto pageBtn : pages)
    {
        if(pageBtn->getPage() == currentPage)
            pageBtn->setActive(true);
        else
            pageBtn->setActive(false);

        if(pageBtn->getPage() == 6)
            pageBtn->setFolded(true);
        else
            pageBtn->setFolded(false);
    }
}

void Paginator::jumpToPageCase2()
{
    pages[0]->setPage(1);
    pages[1]->setPage(pageCount-5);
    pages[2]->setPage(pageCount-4);
    pages[3]->setPage(pageCount-3);
    pages[4]->setPage(pageCount-2);
    pages[5]->setPage(pageCount-1);
    pages[6]->setPage(pageCount);

    for(auto pageBtn : pages)
    {
        if(pageBtn->getPage() == currentPage)
            pageBtn->setActive(true);
        else
            pageBtn->setActive(false);

        if(pageBtn->getPage() == pageCount-5)
            pageBtn->setFolded(true);
        else
            pageBtn->setFolded(false);
    }
}

void Paginator::jumpToPageCase3()
{
    pages[0]->setPage(1);
    pages[1]->setPage(currentPage-2);
    pages[2]->setPage(currentPage-1);
    pages[3]->setPage(currentPage);
    pages[4]->setPage(currentPage+1);
    pages[5]->setPage(currentPage+2);
    pages[6]->setPage(pageCount);

    for(auto pageBtn : pages)
    {
        if(pageBtn->getPage() == currentPage)
            pageBtn->setActive(true);
        else
            pageBtn->setActive(false);

        if(pageBtn->getPage() == currentPage-2 || pageBtn->getPage() == currentPage+2)
            pageBtn->setFolded(true);
        else
            pageBtn->setFolded(false);
    }
}

void Paginator::prevPage()
{
    if(currentPage == 1) {
        return ;
    }
    jumpToPage(currentPage - 1);
}

void Paginator::nextPage()
{
    if(currentPage == pageCount) {
        return ;
    }
    jumpToPage(currentPage + 1);
}

void Paginator::clickPageBtn()
{
    int page = static_cast<PageButton*>(this->sender())->getPage();
    jumpToPage(page);
}
